//
//  LTChangePwdViewController.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/3/30.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "LTChangePwdViewController.h"
#import "LTMineAPIManager.h"


@interface LTChangePwdViewController ()<UITextFieldDelegate ,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *tableHeaderView;

@property(nonatomic ,strong)UIView *tableFootorView;

@property(nonatomic ,strong)UIButton *confirmButton;

@property(nonatomic ,strong)UITextField *passwordTextFiledOld;

@property(nonatomic ,strong)UITextField *passwordTextFiledNew;

@property(nonatomic ,strong)UITextField *passwordTextFiledNewAgain;

@end

@implementation LTChangePwdViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
  
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = LTWhiteColor;
    [self.view addSubview:self.tableView];
    [self setBackButtonIsHidden:NO];

}
#pragma mark -API
-(void)changePwd
{
    NSMutableDictionary *dic = @{@"oldpassword":self.passwordTextFiledOld.text,@"newpassword":self.passwordTextFiledNew.text,@"againpassword":self.passwordTextFiledNewAgain.text}.mutableCopy;
   
    [LTMineAPIManager resetPwdWithParam:dic result:^(NSDictionary * _Nonnull dic) {
        [LTApp goLogin];
    }];

}

#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];
    if(indexPath.row == 0)
    {
        [cell.contentView addSubview:self.passwordTextFiledOld];
    }else if (indexPath.row == 1)
    {
        [cell.contentView addSubview:self.passwordTextFiledNew];
    }else{
        [cell.contentView addSubview:self.passwordTextFiledNewAgain];
    }
    
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
        return 3;
        
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 61;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


}



#pragma mark -UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    //[[textField viewWithTag:1001] setBackgroundColor:kOrangeLineColor];
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    //[[textField viewWithTag:1001] setBackgroundColor:kLineColor];
    
    return  YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}
#pragma mark - private

- (void)changeLoginBtnStatue
{
    BOOL canLogin = NO;
   
    
    
    if(canLogin)
    {
        self.confirmButton.selected = YES;
        self.confirmButton.backgroundColor = LTClearColor;
    }else{
        self.confirmButton.selected = NO;
        self.confirmButton.backgroundColor = rgba(229, 229, 229, 1);
    }
    
}
#pragma mark -event response

- (void)tapClick
{
    [self.view endEditing:YES];
}

- (void)confirmButtonAction
{
    [self.view endEditing:YES];
    if(self.passwordTextFiledNew.text.length > 0&&self.passwordTextFiledOld.text.length > 0&&self.passwordTextFiledNewAgain.text.length > 0)
    {
        [self changePwd];
    }
}


-(void)showMoreAcountAction:(UIButton *)sender
{
    [self.view endEditing:YES];
    sender.selected =!sender.selected;
   
    //[self.captchaWeb loadRequest:self.captchaRequest];
}
-(void)protocolSelectButtonAction:(UIButton *)sender
{
    sender.selected =!sender.selected;
    [self changeLoginBtnStatue];
//    if ([NSString isMatchText:self.phoneTextField.text withPattern:RegexMobileNumber]&&self.protocolSelectButton.selected)
//    {
//        self.registerButton.enabled =YES;
//    }
//    else
//    {
//        self.registerButton.enabled =NO;
//    }
}

-(void)textFieldResignTapAction:(UITapGestureRecognizer *)sender
{
    [self.view endEditing:YES];
}
-(void)textEnd:(UITextField *)textField
{
    [self.tableView reloadData];
}
-(void)textChanged:(UITextField *)textField
{
    
  //  [self changeLoginBtnStatue];
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LTTopBarHeight, LTScreenWidth, LTScreenHeight - LTTopBarHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:LTWhiteColor];
        [_tableView setSeparatorColor:LTClearColor];
        [_tableView setTableHeaderView:self.tableHeaderView];
        [_tableView setTableFooterView:self.tableFootorView];
    }
    
    return _tableView;
}

-(UIView *)tableHeaderView
{
    if (!_tableHeaderView)
    {
        
        _tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, LTScreenWidth,210)];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
            [_tableHeaderView addGestureRecognizer:tap];
    
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, LTScreenWidth - 64, 28)];
        titleLabel.font = MFontWithSize(20);
        titleLabel.text = @"修改密码";
        [titleLabel setTextAlignment:NSTextAlignmentLeft];
        titleLabel.textColor = LTTextBlack;
        
        UILabel * subLabel = [[UILabel alloc] initWithFrame:CGRectMake(32, titleLabel.bottom + 8, LTScreenWidth - 64, 18)];
        subLabel.font = RFontWithSize(13);
        subLabel.text = @"密码不少于8位数，包含大小写字母和数字";
        [subLabel setTextAlignment:NSTextAlignmentLeft];
        subLabel.textColor = rgba(102, 102, 102, 1);
        [_tableHeaderView addSubview:titleLabel];
        [_tableHeaderView addSubview:subLabel];
        
        _tableHeaderView.height = subLabel.bottom + 18;
       
    }
    
    return _tableHeaderView;
}

-(UIView *)tableFootorView
{
    if (!_tableFootorView)
    {
        _tableFootorView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, LTScreenWidth - 20, 20)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [_tableFootorView addGestureRecognizer:tap];
       
        [_tableFootorView addSubview:self.confirmButton];
      
        _tableFootorView.height = self.confirmButton.bottom;
    }
    
    return _tableFootorView;
}

-(UIButton *)confirmButton
{
    if (!_confirmButton)
    {
        _confirmButton = [UIButton new];
        _confirmButton.frame =CGRectMake(32, 36, LTScreenWidth - 64, 44);
       
        [_confirmButton setTitle:@"确 定" forState:UIControlStateNormal];
       // [_confirmButton setTitleColor:rgba(181, 181, 181, 1) forState:UIControlStateNormal];
        [_confirmButton setTitleColor:LTWhiteColor forState:UIControlStateNormal];
        [_confirmButton.titleLabel setFont:MFontWithSize(14)];
        [_confirmButton addTarget:self action:@selector(confirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.backgroundColor = rgba(41, 121, 255, 1);
        _confirmButton.layer.cornerRadius = 22;
        _confirmButton.layer.shadowColor = rgba(41, 121, 255, 0.50).CGColor;
        _confirmButton.layer.shadowOffset = CGSizeMake(0,4);
        _confirmButton.layer.shadowOpacity = 1;
        _confirmButton.layer.shadowRadius = 6;
    }
    
    return _confirmButton;
}
-(UITextField *)passwordTextFiledNewAgain
{
    if (!_passwordTextFiledNewAgain)
    {
        _passwordTextFiledNewAgain =[[UITextField alloc] initWithFrame:CGRectMake(32, 6, LTScreenWidth - 64, 49)];
        _passwordTextFiledNewAgain.font = MFontWithSize(16);
        _passwordTextFiledNewAgain.textColor = rgba(51, 51, 51, 1);
        _passwordTextFiledNewAgain.clearButtonMode =UITextFieldViewModeWhileEditing;
        _passwordTextFiledNewAgain.secureTextEntry =YES;
        _passwordTextFiledNewAgain.delegate =self;
        _passwordTextFiledNewAgain.placeholder = @"确认新密码";
        [_passwordTextFiledNewAgain setAllCornersWithCornerRadius:6];
        _passwordTextFiledNewAgain.backgroundColor = rgba(247, 248, 250, 1);
        //[_passwordTextFiled setInputAccessoryView:self.customTextFieldInputAccessoryView];


        if (@available(iOS 11.0,*)) {
            _passwordTextFiledNewAgain.textContentType =UITextContentTypePassword;
        }

        [_passwordTextFiledNewAgain addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        [_passwordTextFiledNewAgain addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEnd];
        UIView *leftSpaceView = [[UIView alloc] init];
        leftSpaceView.frame = CGRectMake(0,0,15,49);
        leftSpaceView.backgroundColor = rgba(247, 248, 250, 1);
       
        _passwordTextFiledNewAgain.leftViewMode = UITextFieldViewModeAlways;
        _passwordTextFiledNewAgain.leftView = leftSpaceView;
    }

    return _passwordTextFiledNewAgain;
}
-(UITextField *)passwordTextFiledNew
{
    if (!_passwordTextFiledNew)
    {
        _passwordTextFiledNew =[[UITextField alloc] initWithFrame:CGRectMake(32, 6, LTScreenWidth - 64, 49)];
        _passwordTextFiledNew.font = MFontWithSize(16);
        _passwordTextFiledNew.textColor = rgba(51, 51, 51, 1);
        _passwordTextFiledNew.clearButtonMode =UITextFieldViewModeWhileEditing;
        _passwordTextFiledNew.secureTextEntry =YES;
        _passwordTextFiledNew.delegate =self;
        _passwordTextFiledNew.placeholder = @"新密码";
        [_passwordTextFiledNew setAllCornersWithCornerRadius:6];
        _passwordTextFiledNew.backgroundColor = rgba(247, 248, 250, 1);
        //[_passwordTextFiled setInputAccessoryView:self.customTextFieldInputAccessoryView];


        if (@available(iOS 11.0,*)) {
            _passwordTextFiledNew.textContentType =UITextContentTypePassword;
        }

        [_passwordTextFiledNew addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        [_passwordTextFiledNew addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEnd];
        UIView *leftSpaceView = [[UIView alloc] init];
        leftSpaceView.frame = CGRectMake(0,0,15,49);
        leftSpaceView.backgroundColor = rgba(247, 248, 250, 1);
       
        _passwordTextFiledNew.leftViewMode = UITextFieldViewModeAlways;
        _passwordTextFiledNew.leftView = leftSpaceView;
    }

    return _passwordTextFiledNew;
}
-(UITextField *)passwordTextFiledOld
{
    if (!_passwordTextFiledOld)
    {
        _passwordTextFiledOld =[[UITextField alloc] initWithFrame:CGRectMake(32, 6, LTScreenWidth - 64, 49)];
        _passwordTextFiledOld.font = MFontWithSize(16);
        _passwordTextFiledOld.textColor = rgba(51, 51, 51, 1);
        _passwordTextFiledOld.clearButtonMode =UITextFieldViewModeWhileEditing;
        _passwordTextFiledOld.secureTextEntry =YES;
        _passwordTextFiledOld.delegate =self;
        _passwordTextFiledOld.placeholder = @"当前密码";
        [_passwordTextFiledOld setAllCornersWithCornerRadius:6];
        _passwordTextFiledOld.backgroundColor = rgba(247, 248, 250, 1);
        //[_passwordTextFiled setInputAccessoryView:self.customTextFieldInputAccessoryView];


        if (@available(iOS 11.0,*)) {
            _passwordTextFiledOld.textContentType =UITextContentTypePassword;
        }

        [_passwordTextFiledOld addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        [_passwordTextFiledOld addTarget:self action:@selector(textEnd:) forControlEvents:UIControlEventEditingDidEnd];
        UIView *leftSpaceView = [[UIView alloc] init];
        leftSpaceView.frame = CGRectMake(0,0,15,49);
        leftSpaceView.backgroundColor = rgba(247, 248, 250, 1);
       
        _passwordTextFiledOld.leftViewMode = UITextFieldViewModeAlways;
        _passwordTextFiledOld.leftView = leftSpaceView;
    }

    return _passwordTextFiledOld;
}
@end
